import os, collections
from moviepy.editor import VideoFileClip


def file_name(file_dir):
    '''取某个目录下的所有文件（全路径）'''
    '''
    ["D:/shipin/mingmign/tex.txt","D:/shipin/djjdj/tex.txt"]
    '''
    pathss = []
    for root, dirs, files in os.walk(file_dir):
        path = [os.path.join(root, name) for name in files]
        if path:
            pathss.extend(path)
    return pathss


def get_dir_list(file_dir):
    '''递归方式获得全部路径'''
    '''
    一级
        二级
            三级
    '''
    dir_list = os.listdir(file_dir)
    dir_list = [{"name": dir} for dir in dir_list]
    for dir in dir_list:
        file_path = file_dir + '/' + dir['name']
        if os.path.isdir(file_path):
            dir['dir_list'] = get_dir_list(file_path)
        if os.path.isfile(file_path):
            dir['path'] = file_path
    return dir_list


def get_file_time(path):
    '''获取视频时长'''
    clip = VideoFileClip(path)
    size = clip.duration
    clip.reader.close()
    clip.audio.reader.close_proc()

    return size


def convert_time(second):
    '''通过秒数转换时间'''
    M, H = 60, 60 ** 2
    if second < M:
        tim_srt = u'00:00:%02d' % second
    if second < H:
        tim_srt = u'00:%02d:%02d' % (int(second / M), int(second % M))
    else:
        hour = int(second / H)
        mine = int(second % H / M)
        second = int(second % H % M)
        tim_srt = u'%s:%02d:%02d' % (hour, mine, second)
    return tim_srt


def convert_transverse_vertical(level, path_list):
    '''横纵数组转换'''
    column_path = {i: [] for i in range(level - 1)}
    for path in path_list:
        for i in range(level - 1):
            column_path[i].append(path[i])
    return column_path


def get_same_element_list(lst):
    # 获取每组列表中的相同元素的数量字典组成的列表
    dic = dict(collections.Counter(lst))
    return dic


def convert_dict_list(dic):
    # {k:v,k,v}
    # 改成{'k':{k2,v2},'k':{k2,v2},}
    da_dict = {}
    for (k, v) in dict(dic).items():
        da_dict[k] = {'num': v, 'time': 0}
    return da_dict
